package com.u3k.mid;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import com.u3k.app.mid.utils.common.PathUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class d {
    static String a = "LibDownloader";
    static d msInstance;
    private List<String> c = new ArrayList();
    private Map<String, ModuleInfo> d = new HashMap();
    private boolean e;
    private Context f;
    private Handler g = new Handler(Looper.getMainLooper());
    private Object h = new Object();

    public static d getInstance() {
        synchronized (d.class) {
            try {
                if (msInstance == null) {
                    msInstance = new d();
                }
                d dVar = msInstance;
                return dVar;
            } finally {
                Object obj = d.class;
            }
        }
    }

    private String a(Context context, ModuleInfo uVar) {
        return getLibFolder(context) + File.separator + uVar.name + "_" + uVar.libVersion;
    }

    private void a(String str) {
        if (!this.c.contains(str)) {
            this.c.add(str);
        }
    }

    private void a(String str, ModuleInfo uVar) {
        if (str != null && uVar != null) {
            this.d.put(str, uVar);
        }
    }

    private void a(boolean z) {
        this.e = z;
    }

    private ModuleInfo b(String str) {
        return str == null ? null : (ModuleInfo) this.d.get(str);
    }

    private void b() {
        synchronized (this.h) {
            e();
            a(false);
        }
        this.g.post(new f(this));
    }

    private int c() {
        return this.c.size();
    }

    private String getLibFolder(Context context) {
        String str = PathUtil.getFilesDir(context) + File.separator + "temp_libs";
        File file = new File(str);
        if (!file.exists()) {
            file.mkdirs();
        }
        return str;
    }

    private String d() {
        return this.c.size() == 0 ? "" : (String) this.c.get(0);
    }

    private void e() {
        if (this.c.size() > 0) {
            this.c.remove(0);
        }
    }

    private boolean f() {
        return this.e;
    }

    public void a(Context context) {
        LogUtils.LogError(a, "clearTempDownloadPath");
        PathUtil.deleteFiles(getLibFolder(context));
    }

    public void a(ModuleInfo uVar) {
        if (uVar == null) {
            LogUtils.LogError(a, "addDownload  lib=null");
            return;
        }
        synchronized (this.h) {
            LogUtils.LogError(a, "addDownload  module=" + uVar.name + ", id=" + uVar.libId + ", version=" + uVar.libVersion + ", crc32=" + uVar.crc32 + ", url=" + uVar.libUrl);
            ModuleInfo b = b(uVar.name);
            if (b != null) {
                LogUtils.LogError(a, "addDownload  old  module=" + uVar.name + ", id=" + uVar.libId + ", version=" + uVar.libVersion + ", crc32=" + uVar.crc32 + ", url=" + uVar.libUrl);
                b.crc32 = uVar.crc32;
                b.libId = uVar.libId;
                b.libUrl = uVar.libUrl;
                b.libVersion = uVar.libVersion;
            } else {
                a(uVar.name, uVar);
            }
            a(uVar.name);
        }
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public void b(android.content.Context r9) {
        /*
        r8 = this;
        r0 = a;
        r1 = "startDownload  ";
        com.u3k.mid.au.b(r0, r1);
        r1 = r8.h;
        monitor-enter(r1);
        r0 = r8.c();	 Catch:{ all -> 0x0028 }
        if (r0 != 0) goto L_0x0019;
    L_0x0010:
        r0 = a;	 Catch:{ all -> 0x0028 }
        r2 = "startDownload  download size=0";
        com.u3k.mid.au.b(r0, r2);	 Catch:{ all -> 0x0028 }
        monitor-exit(r1);	 Catch:{ all -> 0x0028 }
    L_0x0018:
        return;
    L_0x0019:
        r0 = r8.libUrl();	 Catch:{ all -> 0x0028 }
        if (r0 == 0) goto L_0x002b;
    L_0x001f:
        r0 = a;	 Catch:{ all -> 0x0028 }
        r2 = "startDownload  is downloading";
        com.u3k.mid.au.b(r0, r2);	 Catch:{ all -> 0x0028 }
        monitor-exit(r1);	 Catch:{ all -> 0x0028 }
        goto L_0x0018;
    L_0x0028:
        r0 = move-exception;
        monitor-exit(r1);	 Catch:{ all -> 0x0028 }
        throw r0;
    L_0x002b:
        if (r9 != 0) goto L_0x0036;
    L_0x002d:
        r0 = a;	 Catch:{ all -> 0x0028 }
        r2 = "startDownload  ctx is null";
        com.u3k.mid.au.b(r0, r2);	 Catch:{ all -> 0x0028 }
        monitor-exit(r1);	 Catch:{ all -> 0x0028 }
        goto L_0x0018;
    L_0x0036:
        r0 = r9.getApplicationContext();	 Catch:{ all -> 0x0028 }
        r8.libUrl = r0;	 Catch:{ all -> 0x0028 }
        r0 = r8.d();	 Catch:{ all -> 0x0028 }
        r0 = r8.b(r0);	 Catch:{ all -> 0x0028 }
        if (r0 == 0) goto L_0x004a;
    L_0x0046:
        r2 = r0.libUrl;	 Catch:{ all -> 0x0028 }
        if (r2 != 0) goto L_0x004f;
    L_0x004a:
        r8.b();	 Catch:{ all -> 0x0028 }
        monitor-exit(r1);	 Catch:{ all -> 0x0028 }
        goto L_0x0018;
    L_0x004f:
        r2 = 1;
        r8.a(r2);	 Catch:{ all -> 0x0028 }
        monitor-exit(r1);	 Catch:{ all -> 0x0028 }
        r1 = new com.u3k.app.mid.utils.net.HttpClient;
        r1.<init>();
        r2 = new com.u3k.app.mid.utils.net.Request;
        r2.<init>();
        r3 = r0.libUrl;
        r2 = r2.url(r3);
        r3 = r8.libUrl;
        r3 = r8.a(r3, r0);
        r4 = new java.io.File;
        r4.<init>(r3);
        r4 = r4.exists();
        if (r4 == 0) goto L_0x009c;
    L_0x0075:
        r4 = r0.c;
        r6 = com.u3k.mid.al.a(r3);
        r4 = (r4 > r6 ? 1 : (r4 == r6 ? 0 : -1));
        if (r4 != 0) goto L_0x009c;
    L_0x007f:
        r0 = a;
        r1 = new java.lang.StringBuilder;
        r1.<init>();
        r2 = "startDownload  lib exist, path=";
        r1 = r1.append(r2);
        r1 = r1.append(r3);
        r1 = r1.toString();
        com.u3k.mid.au.b(r0, r1);
        r8.b();
        goto L_0x0018;
    L_0x009c:
        r4 = a;
        r5 = new java.lang.StringBuilder;
        r5.<init>();
        r6 = "startDownload  save path=";
        r5 = r5.append(r6);
        r5 = r5.append(r3);
        r5 = r5.toString();
        com.u3k.mid.au.b(r4, r5);
        r4 = new com.u3k.mid.e;
        r4.<init>(r8, r0);
        r1.download(r2, r3, r4);
        goto L_0x0018;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.u3k.mid.d.b(android.content.Context):void");
    }
}
